package org.example.chatroom.demos.service;

import lombok.extern.slf4j.Slf4j;
import org.example.chatroom.demos.mapper.MessageMapper;
import org.example.chatroom.demos.mapper.MessageSessionMapper;
import org.example.chatroom.demos.mapper.UserMapper;
import org.example.chatroom.demos.model.Friend;
import org.example.chatroom.demos.model.Message;
import org.example.chatroom.demos.model.MessageSession;
import org.example.chatroom.demos.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.*;

@Slf4j
@Service
public class MessageSessionService {
    @Autowired
    private MessageSessionMapper messageSessionMapper ;

    @Autowired
    private MessageMapper messageMapper ;

    @Autowired
    private UserMapper userMapper ;

    public Object getSessionList(HttpSession httpSession) {
        if(httpSession == null ){
            log.info("session为null");
            return null ;
        }
        User user = (User) httpSession.getAttribute("USER_KEY");
        if(user == null || user.getId() < 1){
            log.info("user为null");
            return null ;
        }
        // 获得所有会话id
        List<MessageSession>  messageSessionList = new ArrayList<>() ;
        List<Integer> sessionList = messageSessionMapper.getSessionIdByUserId(user.getId());
        for(int sessionId : sessionList){
            MessageSession messageSession = new MessageSession()  ;
            messageSession.setSessionId(sessionId);
            Friend friend = messageSessionMapper.getFriendBySessionId(sessionId , user.getId());
            messageSession.setFriend(friend);
             String lastContent = messageMapper.getLastContentBySessionId(sessionId) ;
             if(lastContent == null){
//                 如果没有消息,那么就是空
                 messageSession.setLastMessage("");
             }else{
                 if(lastContent.length() > 10){
                  lastContent = lastContent.substring(0 ,14) ;
                 }
                 messageSession.setLastMessage(lastContent);
             }
            messageSessionList.add(messageSession);
        }
        return messageSessionList ;
    }

    @Transactional(rollbackFor = Exception.class)
    public Object createSession(Integer friendId, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("USER_KEY");
        if(user == null || user.getId() < 1) {
            log.info("user为null");
            return null;
        }
         if(friendId == null || friendId < 1){
            log.info("friendId 不存在");
            return null ;
        }
          MessageSession messageSession = new MessageSession();

         // 插入的得到sessionId
         Integer result = messageSessionMapper.insertSession(messageSession);
         if(result < 1){
             return null ;
         }
            result = messageSessionMapper.insertSessionUser(messageSession.getSessionId() ,user.getId()) ;
         if(result<1)return  null ;

           result = messageSessionMapper.insertSessionUser(messageSession.getSessionId() , friendId);
           if(result<1)return  null ;
        Map<String ,Integer> resp = new HashMap<>() ;
        resp.put("sessionId", messageSession.getSessionId());
        return  resp ;
    }

    public Object getHistoryMessage(Integer sessionId) {
        if(sessionId == null || sessionId< 1){
            log.info("sessionId == null");
            return false ;
        }
        List<Message> messagesList = messageMapper.getHistoryMessage(sessionId);
//        针对查出来的结果,翻转过来
        Collections.reverse(messagesList);
        return messagesList;
    }
}
